using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Data;
using System.Windows;
using System.Globalization;

namespace MyPublisher.Windows.Controls.Primitives
{
    [ValueConversion(typeof(DateTime?), typeof(string))]
    public class DateTimeObject2StringConverter : IValueConverter
    {
        public static DateTimeObject2StringConverter Instance
        {
            get { return InstanceOf<DateTimeObject2StringConverter>.GetInstance(); }
        }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null || value == DBNull.Value)
                return string.Empty;
            else
                return ((DateTime)value).ToShortDateString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return null;

            DateTime result;
            if (DateTime.TryParse((string)value, out result))
                return result;
            else
                return null;
        }

        #endregion
    }
}
